﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================
using System.Data.Common;
using XCI.Data.Entity;

namespace XCI.Data.Provider
{
    /// <summary>
    /// SQLite数据库实现
    /// </summary>
    public class SQLiteProvider : BaseProvider
    {
        private static DbProviderFactory factory;

        /// <summary>
        /// 获取数据对象创建工厂
        /// </summary>
        /// <returns></returns>
        public override DbProviderFactory GetDbProviderFactory()
        {
            return factory ?? (factory = GetFactory("System.Data.SQLite.SQLiteFactory,System.Data.SQLite"));
        }

        /// <summary>
        /// 获取参数前缀
        /// </summary>
        public override string GetParamPrefix()
        {
            return "@";
        }

        /// <summary>
        /// 获取Insert语句模板
        /// </summary>
        /// <param name="entityData">实体信息</param>
        /// <param name="cmd">Insert命令对象</param>
        public override void PrepInsert(EntityData entityData, DbCommand cmd)
        {
            if (entityData.PrimaryAttr == null || !entityData.PrimaryAttr.Auto) return;
            cmd.CommandText += ";SELECT last_insert_rowid();";
        }

        /// <summary>
        /// 获取分页语句模板
        /// </summary>
        /// <param name="sql">查询语句</param>
        /// <param name="orderBy"></param>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        /// <returns>获取分页语句模板</returns>
        public override string GetPageStatement(string sql, string orderBy, int startIndex, int endIndex)
        {
            return $"{sql} {orderBy} LIMIT {startIndex},{endIndex - startIndex}";
        }
    }
}